﻿using System;
using System.Data;
using System.Collections.Generic;

using Clipper.Model;
using System.Collections.ObjectModel;
namespace Clipper.BLL
{
	/// <summary>
	/// TestTubeSelfInfo
	/// </summary>
	public partial class TestTubeSelfInfo
	{
		private readonly Clipper.DAL.TestTubeSelfInfo dal=new Clipper.DAL.TestTubeSelfInfo();

        TestTubeInfo mTestTubeBll = new TestTubeInfo();
        SampleInfo mSampleInfoBll = new SampleInfo();
		public TestTubeSelfInfo()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int testTubeSelfID)
		{
			return dal.Exists(testTubeSelfID);
		}
        /// <summary>
        /// 是否存在该序列号
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns></returns>
        public bool ExistBarcode(string barcode)
        {
            return dal.ExistBarcode(barcode);
        }
		/// <summary>
		/// 增加一条数据  如果没有样本，自动添加样本进行保存
		/// </summary>
		public bool Add(Clipper.Model.TestTubeSelfInfo model)
		{
            bool res= false;
            if (dal.Add(model))
            {
                int shelfID= dal.GetMaxId();
                foreach (Model.TestTubeInfo tube in model.TestTubeInfoList)
                {
                    tube.TestTubeSelfID = shelfID;
                    res= mTestTubeBll.Add(tube);
                    //if (!mSampleInfoBll.Exists(tube.SampleID))//添加样本
                    //{
                    //    tube.SampleInfo.sampleID = tube.SampleID;
                    //    tube.SampleInfo.DetectItems = tube.DetectItems;
                    //    mSampleInfoBll.Add(tube.SampleInfo);
                    //}

                    if(!res)
                        return res;
                }
            }
            return res;
		}

		/// <summary>
		/// 更新一条数据,更新试管信息
		/// </summary>
		public bool Update(Clipper.Model.TestTubeSelfInfo model)
		{
            bool res = false;

            if (dal.Update(model))
            {
                foreach (Model.TestTubeInfo tube in model.TestTubeInfoList)
                {
                    res = mTestTubeBll.Update(tube);

                    //if (!mSampleInfoBll.Exists(tube.SampleID))//添加样本
                    //{
                    //    tube.SampleInfo.sampleID = tube.SampleID;
                    //    tube.SampleInfo.DetectItems = tube.DetectItems;
                    //    res= mSampleInfoBll.Add(tube.SampleInfo);
                    //}
                    //else//更新样本
                    //{
                    //    tube.SampleInfo.sampleID = tube.SampleID;
                    //    tube.SampleInfo.DetectItems = tube.DetectItems;
                    //    res=mSampleInfoBll.Update(tube.SampleInfo);
                    //}

                    if (!res)
                        break;
                }
            }
            return res;
		}
     
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int testTubeSelfID)
		{
			return dal.Delete(testTubeSelfID);
		}
        /// <summary>
        /// 删除样本架
        /// </summary>
        /// <param name="barcode">样本架条码</param>
        /// <returns>是否成功</returns>
        public bool Delete(string barcode)
        {
            return dal.Delete(barcode);
        }
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string testTubeSelfIDlist )
		{
			return dal.DeleteList(testTubeSelfIDlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.TestTubeSelfInfo GetModel(int testTubeSelfID)
		{
			
			return dal.GetModel(testTubeSelfID);
		}

		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public ObservableCollection<Clipper.Model.TestTubeSelfInfo> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}


		/// <summary>
		/// 获得数据列表
		/// </summary>
		public ObservableCollection<Clipper.Model.TestTubeSelfInfo> DataTableToList(DataTable dt)
		{
			ObservableCollection<Clipper.Model.TestTubeSelfInfo> modelList = new ObservableCollection<Clipper.Model.TestTubeSelfInfo>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Clipper.Model.TestTubeSelfInfo model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = dal.DataRowToModel(dt.Rows[n]);
					if (model != null)
					{
						modelList.Add(model);
					}
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  BasicMethod
		#region  ExtensionMethod
        /// <summary>
        /// 获得所有未测试管架列表,包含试管,样本，病人信息
        /// </summary>
        public ObservableCollection<Clipper.Model.TestTubeSelfInfo> GetFullModelList()
        {
            DataSet ds = dal.GetList("testTubeSelfState=0");
            ObservableCollection<Clipper.Model.TestTubeSelfInfo> modelList = new ObservableCollection<Clipper.Model.TestTubeSelfInfo>();
            int rowsCount = ds.Tables[0].Rows.Count;
            if (rowsCount > 0)
            {
                Clipper.Model.TestTubeSelfInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(ds.Tables[0].Rows[n]);
                    if (model != null)
                    {
                        //获取试管，样本，检测项
                        model.TestTubeInfoList = mTestTubeBll.GetModelListByShelf(model);
                        modelList.Add(model);
                        
                    }
                }
            }
            return modelList;
        }

        /// <summary>
        /// 保存试管架信息
        /// </summary>
        /// <param name="shelfList"></param>
        /// <returns></returns>
        public bool SaveTubeShelfList(ObservableCollection<Model.TestTubeSelfInfo> shelfList)
        {
            bool result = false;
            foreach (Model.TestTubeSelfInfo shelf in shelfList)
            {
                if (this.Exists(shelf.testTubeSelfID))
                {
                    result = this.Update(shelf);
                }
                else
                {
                    result = this.Add(shelf);
                }
                if (result == false)
                    break;
            }
            return result;
        }
        /// <summary>
        /// 保存试管架
        /// </summary>
        /// <param name="shelfList"></param>
        /// <returns></returns>
        public bool SaveTubeShelfList(Model.TestTubeSelfInfo shelfList)
        {
            bool result = false;
            if (this.Exists(shelfList.testTubeSelfID))
            {
                result = this.Update(shelfList);
            }
            else
            {
                result = this.Add(shelfList);
            }

            return result;
        }

        /// <summary>
        /// 删除试管架信息,删除试管信息,删除没有病人信息的样本
        /// </summary>
        /// <param name="shelfList"></param>
        /// <returns></returns>
        public bool DeleteTubeShelfList(Model.TestTubeSelfInfo shelf)
        {
            bool result = false;
            //foreach (Model.TestTubeSelfInfo shelf in shelfList)
            //{
                result = this.Delete(shelf.testTubeSelfID);
                
                foreach (Model.TestTubeInfo tube in shelf.TestTubeInfoList)
                {
                    mTestTubeBll.Delete(tube.testTubeID);
                    if (tube.SampleInfo.PatientInfo.name == null)
                    {
                        mSampleInfoBll.Delete(tube.SampleID);
                    }
                    if (result == false)
                        break;
                }
            //}
            return result;
        }

		#endregion  ExtensionMethod
	}
}

